package com.boot.controller;

import com.boot.service.UserService;
import com.boot.utils.JwtHelper;
import com.boot.utils.Result;
import com.boot.pojo.User;
import com.boot.utils.ResultCodeEum;
import io.micrometer.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("user")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtHelper jwtHelper;

    @GetMapping
    public String index() {
        return "Hello WelCome";
    }

    @PostMapping("login")
    public Result login(@RequestBody User user) {
        return userService.login(user);
    }

    @GetMapping("getUserInfo")
    public Result getUserInfo(@RequestHeader String token) {
        return userService.getUserInfo(token);
    }

    @RequestMapping("checkUserName")
    public Result checkUserName(String username) {
        return userService.checkUserName(username);
    }

    @PostMapping("register")
    public Result register(@RequestBody User user) {
        return userService.register(user);
    }


    @GetMapping("checkLogin")
    public Result<Object> checkLogin(@RequestHeader String token) {
        if (StringUtils.isEmpty(token)) {
            return Result.build(null, ResultCodeEum.NOTLOGIN);
        }
        if (jwtHelper.isExpiration(token)) {
            return Result.build(null, ResultCodeEum.NOTLOGIN);
        }
        return Result.ok(null);
    }

    @GetMapping("user/refreshToken")
    public Result refreshToken(@RequestHeader String refreshToken) {
        return userService.getRefreshToken(refreshToken);
    }
}
